package com.sanpu.kxe.common.utils;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sanpu.kxe.common.view.SelectPopWindow;
import com.sanpu.kxe.module.wheel.DateWheel;
import com.sanpu.kxe.module.wheel.interfaces.IWheelDataChangeCallback;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description：日期弹框选择器
 * Created by 薛勇军 on 2019/9/24.
 */

public class DateCheckPopListener {

    private static final int INDEX_YEAR = 0;

    private static final int INDEX_MONTH = 1;

    private static final int INDEX_DAY = 2;

    private String DEFAULT_DATE;

    private final Activity context;

    public DateCheckPopListener(Activity context) {
        this.context = context;
        DEFAULT_DATE = "2017-01-01";
    }

    public DateCheckPopListener(Activity context, boolean currentTimeType) {
        this.context = context;
        if (currentTimeType) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            DEFAULT_DATE = format.format(date);
        } else {
            DEFAULT_DATE = "2017-01-01";
        }
    }

    /**
     * 处理滚轮选择事件
     *
     * @param parentView 父控件
     * @param showView   显示的控件
     */
    public void handleRegionCheck(View parentView, TextView showView, boolean dayVisible) {
        handleRegionCheck(parentView, showView, dayVisible, null);
    }

    public void handleRegionCheck(View parentView, TextView showView, boolean dayVisible,
                                  IWheelDataChangeCallback iWheelDataChangeCallback) {
        if (parentView == null) {
            return;
        }
        String showBirthday;
        if (showView.getText() != null && !TextUtils.isEmpty(showView.getText().toString())) {
            showBirthday = showView.getText().toString();
        } else {
            showBirthday = DEFAULT_DATE;
        }
        DateWheel dwView;
        if (!TextUtils.isEmpty(showBirthday) && showBirthday.contains("-")) {
            String[] split = showBirthday.split("-");
            if (dayVisible)
                dwView = new DateWheel(context, null, Integer.parseInt(split[INDEX_YEAR]),
                        Integer.parseInt(split[INDEX_MONTH]), Integer.parseInt(split[INDEX_DAY]));
            else
                dwView = new DateWheel(context, null, Integer.parseInt(split[INDEX_YEAR]),
                        Integer.parseInt(split[INDEX_MONTH]), 1);
        } else {
            dwView = new DateWheel(context);
        }

        SelectPopWindow pw = new SelectPopWindow(context, parentView,
                dwView.getTimeView());
        MyClickListener regionClickListener = new MyClickListener(pw, showView,
                iWheelDataChangeCallback);
        dwView.setWheelFinishListener(regionClickListener);
        dwView.setDayVisible(dayVisible);
        pw.setConfirmListener(regionClickListener);
    }

    class MyClickListener implements DateWheel.OnWheelFinishListener, View.OnClickListener {

        private final TextView showView;

        private String wheelData;

        private final IWheelDataChangeCallback iWheelDataChangeCallback;

        private final SelectPopWindow pw;

        public MyClickListener(SelectPopWindow pw, TextView showView,
                               IWheelDataChangeCallback iWheelDataChangeCallback) {
            this.showView = showView;
            this.pw = pw;
            this.iWheelDataChangeCallback = iWheelDataChangeCallback;
        }

        @Override
        public void onClick(View v) {
            if (showView != null) {
                showView.setText(wheelData);
            }
            if (iWheelDataChangeCallback != null) {
                iWheelDataChangeCallback.wheelDataChangeCallback(wheelData);
            }
            pw.dismiss();
        }

        @Override
        public void onChange(String currentValue) {
            this.wheelData = currentValue;
        }
    }
}
